// 获取页面尺寸，用于判断是移动端还是PC端
let pageWidth = document.body.clientWidth;
let verificationCodeBtn,loginBtn,pswLoginBtn,registerBtn = "";
// 根据响应式布局分别获取数据
if (pageWidth >= 768) {
    verificationCodeBtn = document.getElementsByClassName("getVerificationCode")[0];
    loginBtn = document.getElementsByClassName("loginBtn")[0];
    pswLoginBtn = document.getElementsByClassName("pswLoginBtn")[0];
    registerBtn = document.getElementsByClassName("registerBtn")[0];
}else{
    verificationCodeBtn = document.getElementsByClassName("getVerificationCode")[1];
    loginBtn = document.getElementsByClassName("loginBtn")[1];
    pswLoginBtn = document.getElementsByClassName("pswLoginBtn")[1];
    registerBtn = document.getElementsByClassName("registerBtn")[1];
}

let verCode = 0;
let count = 60;

verificationCodeBtn.addEventListener('click', () => {
    let inputEmail = "";
    // 根据响应式布局分别获取数据
    if (pageWidth >= 768) {
        inputEmail = document.getElementsByClassName("email")[0].value;
    }else{
        inputEmail = document.getElementsByClassName("email")[1].value;
    }
    if (inputEmail.length === 0) {
        alertBySweet("邮箱不能为空");
    } else if (inputEmail.indexOf("@") == -1||inputEmail.indexOf(".com") == -1) {
        alertBySweet("请输入正确格式的邮箱！");
    } else {
        let timer = setInterval(function () {
            verificationCodeBtn.innerHTML = "剩余" + count + "s";
            count--;
            if (count === -1) {
                verificationCodeBtn.innerHTML = "获取验证码";
                clearInterval(timer);
                count = 60;
            }
        }, 1000);
        //给邮箱发送验证码
        if (count === 60) {// 60秒发送一次验证码
            ajax({
                type: 'POST',
                url: api.sendEmail,
                async: true,
                contentType: "application/json;charset=utf-8",
                data: {
                    email: inputEmail,
                },
                success: function (data) {
                    let res = JSON.parse(data);
                    if (res.statusCode === 200) {
                        alertBySweet("邮件发送成功，请查收！");
                        verCode = res.data.verCode;
                    } else {
                        alertBySweet("邮件发送失败，请稍后重试！");
                    }
                },
                error: function () {
                    alertBySweet("发送失败");
                }
            });
        }
    }
});

loginBtn.addEventListener('click', function () {
    let inputVerCode,inputEmail = "";
    // 根据响应式布局分别获取数据
    if (pageWidth >= 768) {
        inputVerCode = document.getElementsByClassName("verCode")[0].value;
        inputEmail = document.getElementsByClassName("email")[0].value;//防止用户发完验证码之后又改了邮箱
    }else{
        inputVerCode = document.getElementsByClassName("verCode")[1].value;
        inputEmail = document.getElementsByClassName("email")[1].value;//防止用户发完验证码之后又改了邮箱
    }
    if (inputVerCode === verCode) {//验证码正确
        //给邮箱发送验证码
        ajax({
            type: 'POST',
            url: api.emailLogin,
            // url: '/api/emailLogin',
            async: true,
            contentType: "application/json;charset=utf-8",
            data: {
                email: inputEmail,
            },
            success: function (data) {
                let res = JSON.parse(data);
                if (res.statusCode === 200) {
                    alertBySweet("登录成功！");
                    sessionStorage.setItem("userEmail",inputEmail);
                    location.href = route.noviceGuidance;
                } else {
                    alertBySweet("登录失败，该邮箱未注册！");
                }
            },
            error: function () {
                alertBySweet("登录失败");
            }
        });
    } else {
        alertBySweet("验证码输入错误！");
    }
});

/*跳转至密码登录页面*/
pswLoginBtn.addEventListener('click', () => {
    location.href = route.pswLogin;
});

/*跳转至注册页面*/
registerBtn.addEventListener('click',() => {
    location.href = route.register;
})